let questionsApp = new Vue({
    el: '#questionsApp',
    data:{
        questions: [],
        pageInfo: {}
    },
    methods:{
        loadQuestions:function(pageNum){
            if(pageNum == null){
                pageNum = 1;
            }
            $.ajax({
                url: "/v1/questions/teacher",
                method: 'get',
                data: {'pageNum':pageNum},
                success:function(r){
                    console.log("获取登陆老师的问题" + r.data.list);
                    console.log("分页信息----------" + questionsApp.pageInfo);
                    if(r.code === OK){
                        questionsApp.questions = r.data.list
                        questionsApp.pageInfo = r.data
                        //为questions添加持续时间属性
                        questionsApp.updateDuration();
                        questionsApp.updateTagImage();
                    }
                }
            })
        },
        updateTagImage:function(){
            let questions = this.questions;
            for(let i=0;i<questions.length;i++){
                //获取 tags
                let tags = questions[i].tags;
                //拿到每个tag的id
                if(tags){
                    //拼接成图片的地址let tagImage = /img/tags/+tag[0].id +.jpg
                    let tagImage = '/img/tags/' + tags[0].id + '.jpg';
                    questions[i].tagImage = tagImage;
                }

            }

        },
        updateDuration:function(){
            let questions = this.questions;
            for(let i=0;i<questions.length;i++){
                //创建问题的时间 ms
                let createtime = new Date(questions[i].createtime).getTime(); // 创建时间 -- ms
                //当前时间
                let now = new Date().getTime();
                let duration = now - createtime;
                if(duration < 60*1000){
                    questions[i].duration = '刚刚';
                }else if(duration < 60*60*1000){
                    questions[i].duration = (duration/1000/60).toFixed(0) + '分钟以前';
                }else if(duration < 24*60*60*1000){
                    questions[i].duration = (duration/1000/60/60).toFixed(0) + '小时以前';
                }else {
                    questions[i].duration = (duration/1000/60/60/24).toFixed(0) + '天以前';
                }
            }

        }
    },
    created:function(){
        this.loadQuestions();
    }
})